package com.jiulan.hearthearing.common;

import android.app.Activity;
import android.content.Context;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.jiulan.hearthearing.base.ActivityCollector;
import com.jiulan.hearthearing.constants.Constants;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.login.activity.LoginVerificationCodeActivity;
import com.jiulan.hearthearing.ui.main.MainActivity;
import com.jiulan.hearthearing.ui.my.model.UserInfoModel;
import com.jiulan.hearthearing.utils.PackageInfoUtils;
import com.jiulan.hearthearing.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.Map;

@RequiresApi(api = Build.VERSION_CODES.M)
public class MethodUtils {
    /**
     * 获取固定参数
     */
    public static Map getParams(Context context) {
        Map<String, String> map = new HashMap();
        map.put("token", getToken(context));
        return map;
    }

    /**
     * 获取token
     */
    public static String getToken(Context context) {
        String token = "";
        if (SharedPreferencesUtils.hasValue(context, Constants.ShareKey_Token)) {
            token = SharedPreferencesUtils.getSharedPreferences(context, Constants.ShareKey_Token);
        }
        return token;
    }

    /**
     * 获取登录账号
     */
    public static String getAccount(Context context) {
        String loginName = "";
        if (SharedPreferencesUtils.hasValue(context, Constants.ShareKey_account)) {
            loginName = SharedPreferencesUtils.getSharedPreferences(context, Constants.ShareKey_account);
        }
        return loginName;
    }

    /**
     * 登录之后
     *
     * @param context
     */
    public static void loginAfter(final Activity context, final String loginName, final String token) {
        Map<String, String> map1 = new HashMap<>();
        map1.put(Constants.ShareKey_account, loginName);
        map1.put(Constants.ShareKey_Token, token);
        SharedPreferencesUtils.saveSharedPreferences(context, map1);
        getUserInfo(context);
    }

    /**
     * 获取会员信息
     *
     * @param context
     */
    public static void getUserInfo(final Activity context) {
        Map<String, String> map = new HashMap<>();
        new HttpRequest(context).doGet(UrlConstants.user_info, null, map, UserInfoModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof UserInfoModel) {
                    UserInfoModel model = (UserInfoModel) result;
                    UserData.create(context).saveUserData(model.getData());
                    MainActivity.actionStart(context, 0);
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    /**
     * 退出登录之后
     *
     * @param context
     */
    public static void logoutAfter(final Context context) {
        SharedPreferencesUtils.delSharedPreferences(context, Constants.ShareKey_account);
        SharedPreferencesUtils.delSharedPreferences(context, Constants.ShareKey_Token);
        UserData.create(context).ChangeLoginState(false);
        LoginVerificationCodeActivity.actionStart(context, "");
        ActivityCollector.finishAll();
    }

    // 判断是否升级
    public static boolean isUpData(Context context, String serverVersionCode) {
        if ("".equals(serverVersionCode)) {
            return false;
        }
        try {
            serverVersionCode = serverVersionCode.replace(".", "");
            String name = PackageInfoUtils.getVersionName(context).replace(".", "");
            if (Integer.parseInt(name) < Integer.parseInt(serverVersionCode)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
